<?php
use_helper('I18N');
/** @var Array of menu items */ $items = $sf_data->getRaw('items');
/** @var Array of categories, each containing an array of menu items and settings */ $categories = $sf_data->getRaw('categories');
use_stylesheet("/sfAdminThemejRollerPlugin/css/reset.css", "first");
use_stylesheet("/sfAdminThemejRollerPlugin/css/fg.menu.css");
use_stylesheet("/sfAdminThemejRollerPlugin/css/jquery/ui-darkness/jquery-ui.custom.css");
use_stylesheet("/sfAdminThemejRollerPlugin/css/jroller.css");
use_stylesheet("/sfAdminThemejRollerPlugin/css/fg.buttons.css");
use_stylesheet("/sfAdminThemejRollerPlugin/css/ui.selectmenu.css");

use_javascript("/sfAdminThemejRollerPlugin/js/fg.menu.js");
use_javascript("/sfAdminThemejRollerPlugin/js/jroller.js");
use_javascript("/sfAdminThemejRollerPlugin/js/ui.selectmenu.js");
?>
<?php use_helper('jQuery') ?>
<?php use_helper('Url') ?>
<?php
echo javascript_tag( jq_remote_function(array(
    'update'    => 'queueImport',
    'url'       => url_for("sfJqueryDash/queueImport"),
    'complete'  => 'updateTaskImport();',
)));
echo jq_periodically_call_remote(array(
    'frequency' => 60,
    'update'    => 'queueImport',
    'url'       => url_for("sfJqueryDash/queueImport"),
    'complete'  => 'updateTaskImport();',
    'before'    => '$("#queueImport").html("Checking...");',
));
echo javascript_tag( jq_remote_function(array(
    'update'    => 'queueArmory',
    'url'       => url_for("sfJqueryDash/queueArmory"),
    'complete'  => 'updateTaskArmory();',
)));
echo jq_periodically_call_remote(array(
    'frequency' => 60,
    'update'    => 'queueArmory',
    'url'       => url_for("sfJqueryDash/queueArmory"),
    'complete'  => 'updateTaskArmory();',
    'before'    => '$("#queueArmory").html("Checking...");',
));
echo javascript_tag( jq_remote_function(array(
    'update'    => 'serviceImport',
    'url'       => url_for("sfJqueryDash/serviceImport"),
    'complete'  => 'updateTaskImport();',
)));
echo jq_periodically_call_remote(array(
    'frequency' => 60,
    'update'    => 'serviceImport',
    'url'       => url_for("sfJqueryDash/serviceImport"),
    'complete'  => 'updateTaskImport();',
    'before'    => '$("#serviceImport").html("Checking...");',
));
echo javascript_tag( jq_remote_function(array(
    'update'    => 'serviceArmory',
    'url'       => url_for("sfJqueryDash/serviceArmory"),
    'complete'  => 'updateTaskArmory();',
)));
echo jq_periodically_call_remote(array(
    'frequency' => 60,
    'update'    => 'serviceArmory',
    'url'       => url_for("sfJqueryDash/serviceArmory"),
    'complete'  => 'updateTaskArmory();',
    'before'    => '$("#serviceArmory").html("Checking...");',
));
?>
<script type="text/javascript">
//<![CDATA[
function updateTaskImport()
{
    var queue = parseInt($("#queueImport").text())
    var txt = "";
    if ($("#serviceImport").text() == "STOPPED")
    {
        if (queue > 0)
        {
            txt = '<?php echo str_replace("'", "\\'", jq_link_to_remote("Start Import Service", array(
                'before'    => "var oldHtml = $('#taskImport').html(); $('#taskImport').html('Starting...')",
                'success'   => "$('#taskImport').html(oldHtml)",
                'url'       => url_for("task/startimport"),
            ))) ?><BR/>'
        }
    }
    if (queue > 0)
    {
        $("#queueImport").html(queue + ' <?php echo str_replace("'", "\\'", jq_link_to_remote("[Clear]", array(
                'before'    => "$('#queueImport').html('Clearing...')",
                'success'   => "$('#queueImport').html(0); updateTaskImport();",
                'url'       => url_for("task/clearimport"),
            ))) ?>');
    }
    else
    {
        $("#queueImport").html(queue);
    }
    
    $("#taskImport").html(txt);
}

function updateTaskArmory()
{
    var queue = parseInt($("#queueArmory").text())
    var txt = "";
    if ($("#serviceArmory").text() == "STOPPED")
    {
        if (queue > 0)
        {
            txt = '<?php echo str_replace("'", "\\'", jq_link_to_remote("Start Armory Service", array(
                'before'    => "var oldHtml = $('#taskArmory').html(); $('#taskArmory').html('Starting...')",
                'success'   => "$('#taskArmory').html(oldHtml)",
                'url'       => url_for("task/startarmory"),
            ))) ?><BR/>'
        }
    }
    if (queue > 0)
    {
        $("#queueArmory").html(queue + ' <?php echo str_replace("'", "\\'", jq_link_to_remote("[Clear]", array(
                'before'    => "$('#queueArmory').html('Clearing...')",
                'success'   => "$('#queueArmory').html(0); updateTaskArmory();",
                'url'       => url_for("task/cleararmory"),
            ))) ?>');
    }
    else
    {
        $("#queueArmory").html(queue);
    }

    $("#taskArmory").html(txt);
}
//]]>
//</script>
<div id="" class="dashDiv ui-widget-content ui-corner-all" style="float: left; width: 140px;">
<?php echo "<h1 style='text-align: center;'>".__("Services", null, 'sf_admin_dash')."</h1>"; ?>
    <h3>
        Import: <span id="serviceImport">Checking...</span><BR/>
        Armory: <span id="serviceArmory">Checking...</span>
    </h3>
</div>
<div id="" class="dashDiv ui-widget-content ui-corner-all" style="float: left; width: 340px;">
<?php echo "<h1 style='text-align: center;'>".__("Tasks", null, 'sf_admin_dash')."</h1>"; ?>
    <h3>
        <span id="taskImport"></span>
        <span id="taskArmory"></span>
<?php if ($allowTasks): ?>
        <span id="taskClearImport"><?php echo jq_link_to_remote("Clear Import History", array(
            'before'    => "var oldHtml = $('#taskClearImport').html(); $('#taskClearImport').html('Clearing...')",
            'success'  => "$('#taskClearImport').html(oldHtml)",
            'url'       => url_for("task/clearimporthistory"),
        )) ?></span><BR/>
        <span id="taskClearArmory"><?php echo jq_link_to_remote("Clear Armory History", array(
            'before'    => "var oldHtml = $('#taskClearArmory').html(); $('#taskClearArmory').html('Clearing...')",
            'success'  => "$('#taskClearArmory').html(oldHtml)",
            'url'       => url_for("task/cleararmoryhistory"),
        )) ?></span><BR/>
<?php endif; ?>
        <span id="taskUpdatePoints"><?php echo jq_link_to_remote("Update Points", array(
            'before'    => "var oldHtml = $('#taskUpdatePoints').html(); $('#taskUpdatePoints').html('Updating...')",
            'success'  => "$('#taskUpdatePoints').html(oldHtml)",
            'url'       => url_for("task/updatepoints"),
        )) ?></span><BR/>
        <span id="taskClearCache"><?php echo jq_link_to_remote("Clear Page Cache", array(
            'before'    => "var oldHtml = $('#taskClearCache').html(); $('#taskClearCache').html('Clearing...')",
            'success'  => "$('#taskClearCache').html(oldHtml)",
            'url'       => url_for("task/clearpagecache"),
        )) ?></span><BR/>
    </h3>
</div>
<div id="" class="dashDiv ui-widget-content ui-corner-all" style="float: left; width: 140px;">
<?php echo "<h1 style='text-align: center;'>".__("Queue", null, 'sf_admin_dash')."</h1>"; ?>
    <h3>
        Import: <span id="queueImport">Checking...</span><BR/>
        Armory: <span id="queueArmory">Checking...</span>
    </h3>
</div>
<div id="" class="dashDiv ui-widget-content ui-corner-all" style="clear: left;">
    <?php
    if (count($items))
    {
        include_partial('dash_list', array('items' => $items));
    }
    if (count($categories))
    {
        foreach ($categories as $name => $category)
        {
            if (sfJqueryDash::hasPermission($category, $sf_user))
            {

                echo "<h1>".__(isset($category['name']) ? $category['name'] : $name, null, 'sf_admin_dash')."</h1>";
                include_partial('dash_list', array('items' => $category['items']));
            }
        }
    }
    elseif (!count($items))
    {
        echo __('sfJqueryDashPlugin is not configured.  Please see the %documentation_link%.', array('%documentation_link%'=>link_to(__('documentation', null, 'sf_admin_dash'), 'http://www.symfony-project.org/plugins/sfJqueryDashPlugin?tab=plugin_readme', array('title' => __('documentation', null, 'sf_admin_dash')))), 'sf_admin_dash');
    }
    
    ?>
</div>